package org.songwang.org.service.impl;

import org.songwang.basic.PageList;
import org.songwang.basic.query.BaseQuery;
import org.songwang.org.domain.Department;
import org.songwang.org.domain.vo.DepatmentVO;
import org.songwang.org.mapper.DepartmentMapper;
import org.songwang.org.query.DepartmentQuery;
import org.songwang.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public Department findById(Long id) {
        return (Department) departmentMapper.findById(id);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public PageList findQuery(BaseQuery query) {
        return null;
    }

    @Override
    public Long count(BaseQuery query) {
        return departmentMapper.count(query);
    }



    @Override
    public PageList findQueryVO(DepartmentQuery query) {
        Long count = departmentMapper.count(query);
        List<DepatmentVO> queryVO = departmentMapper.findQueryVO(query);
        return new PageList(count,queryVO);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        departmentMapper.batchRemove(ids);
    }

    @Override
    public List<Department> queryTree() {
        //1.方案1: 基于mybatis的1对多的思路,执行嵌套查询(不推荐)
        //2.方案2: 数据处理,在java中完成

        //1.查询出所有部门
        List<Department> departments = departmentMapper.findAll();
        //把所有部门信息转为map: <部门id:部门对象>
        Map<Long,Department> map = new HashMap<>();
        for(Department d:departments){
            map.put(d.getId(),d);
        }
        //2.找出顶级部门
        List<Department> level1List = new ArrayList<>(); //存储顶级部门
        for(Department dept:departments){
            if(dept.getParent_id()==null){ //父级id为null就是顶级部门
                level1List.add(dept);
            }else {
                //3.剩下的可能是 二级,三级,四级部门...
                //  找到当前部门的父级部门,把自己设置到他的 children 属性上
                Department parent = map.get(dept.getParent_id()); //使用当前部门的父级id查询自己的老爸谁
                if(parent!=null){
                    parent.getChildren().add(dept);
                }
            }
        }
        return level1List;
    }
}
